/*
 * WordCounter.java
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.teamskill.pux.library.ai.textmining;

import java.util.Hashtable;
import java.util.Map;
import java.util.StringTokenizer;

import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.OutputPort;

/**
 * A WordCounter node reads a (textual) string from its input port and creates a
 * String-Integer Map containing the occurences of each word which appeared in
 * the input string.
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class WordCounter extends AbstractNode {

	/**
	 * IN_STRING
	 */
	private static final String IN_STRING = "STRING";

	/**
	 * OUT_MAP
	 */
	private static final String OUT_MAP = "MAP";

	/**
	 * CONFIG_DELIM
	 */
	private static final String CONFIG_DELIM = "delimiters";

	/**
	 * DEFAULT_DELIMITERS
	 */
	private static final Object DEFAULT_DELIMITERS = " \t\n\r!&*()-_+=[]\\|{}\"';:/?.,<>";

	/**
	 * CONFIG_LOWER
	 */
	private static final String CONFIG_LOWER = "toLower";

	/**
	 * DEFAULT_TOLOWER
	 */
	private static final Boolean DEFAULT_TOLOWER = new Boolean(true);

	/**
	 * in
	 */
	private InputPort in;

	/**
	 * out
	 */
	private OutputPort out;

	/**
	 * delim
	 */
	private InputPort delim;

	/**
	 * toLower
	 */
	private InputPort toLower;

	/**
	 * WordCounter Constructor
	 * 
	 */
	public WordCounter() {
		this("WordCounter" + nr++, false);
	}

	/**
	 * WordCounter Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public WordCounter(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	public void compute() {
		String s = (String) in.fetchData();
		String d = (String) delim.fetchData();
		StringTokenizer st = new StringTokenizer(s, d);
		Map<String, Integer> m = new Hashtable<String, Integer>();
		boolean l = ((Boolean) toLower.fetchData()).booleanValue();
		while (st.hasMoreTokens()) {
			String t = l ? st.nextToken().toLowerCase() : st.nextToken();
			if (m.containsKey(t)) {
				// increment counter for t
				int c = m.get(t).intValue() + 1;
				m.put(t, new Integer(c));
			} else {
				// create new entry for t and set occurence count to 1
				m.put(t, new Integer(1));
			}
		}
		out.pushData(m);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		in = addInputPort(IN_STRING, String.class, true);
		out = addOutputPort(OUT_MAP, Map.class);
		delim = addConfigurationPort(CONFIG_DELIM, String.class, DEFAULT_DELIMITERS);
		toLower = addConfigurationPort(CONFIG_LOWER, Boolean.class, DEFAULT_TOLOWER);
	}

	/**
	 * JavaBean-style getter-method for in
	 * 
	 * @return the in
	 */
	public InputPort getIn() {
		return in;
	}

	/**
	 * /** JavaBean-style setter-method for in
	 * 
	 * @param in
	 *            the in to set
	 */
	public void setIn(InputPort in) {
		this.in = in;
	}

	/**
	 * JavaBean-style getter-method for out
	 * 
	 * @return the out
	 */
	public OutputPort getOut() {
		return out;
	}

	/**
	 * /** JavaBean-style setter-method for out
	 * 
	 * @param out
	 *            the out to set
	 */
	public void setOut(OutputPort out) {
		this.out = out;
	}

	/**
	 * JavaBean-style getter-method for delim
	 * 
	 * @return the delim
	 */
	public InputPort getDelim() {
		return delim;
	}

	/**
	 * /** JavaBean-style setter-method for delim
	 * 
	 * @param delim
	 *            the delim to set
	 */
	public void setDelim(InputPort delim) {
		this.delim = delim;
	}

	/**
	 * JavaBean-style getter-method for toLower
	 * 
	 * @return the toLower
	 */
	public InputPort getToLower() {
		return toLower;
	}

	/**
	 * /** JavaBean-style setter-method for toLower
	 * 
	 * @param toLower
	 *            the toLower to set
	 */
	public void setToLower(InputPort toLower) {
		this.toLower = toLower;
	}

}
